/* 
 * cms，一个基于J2EE架构内容管理系统
 * Copyright ? GrayRabbit Co., Ltd. All rights reserved.
 * Department:运营department
 * 更多信息请访问：
 * http://code.google.com/p/gray-rabbit-cms/
 */
package org.grayrabbit.cms.action.content;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.grayrabbit.cms.action.common.BaseAction;
import org.grayrabbit.cms.constant.ConstantEnum.ContentTypes;
import org.grayrabbit.cms.constant.ConstantEnum.Module;
import org.grayrabbit.cms.constant.ConstantEnum.OperAct;
import org.grayrabbit.cms.constant.ConstantOperCode;
import org.grayrabbit.cms.constant.ConstantWebApp;
import org.grayrabbit.cms.constant.ContentStatic;
import org.grayrabbit.cms.constant.DBInitId;
import org.grayrabbit.cms.constant.DictionaryStaticValues;
import org.grayrabbit.cms.entity.Admin;
import org.grayrabbit.cms.entity.Category;
import org.grayrabbit.cms.entity.Content;
import org.grayrabbit.cms.entity.Dictionary;
import org.grayrabbit.cms.entity.OperLog;
import org.grayrabbit.cms.entity.Site;
import org.grayrabbit.cms.entity.Topic;
import org.grayrabbit.cms.service.category.CategoryService;
import org.grayrabbit.cms.service.category.impl.CategoryCache;
import org.grayrabbit.cms.service.content.ContentAttachService;
import org.grayrabbit.cms.service.content.ContentService;
import org.grayrabbit.cms.service.dictionary.DictionaryService;
import org.grayrabbit.cms.service.system.AdminService;
import org.grayrabbit.cms.service.system.ConfigService;
import org.grayrabbit.cms.service.system.OperLogService;
import org.grayrabbit.cms.service.topic.TopicService;
import org.grayrabbit.cms.util.DateUtil;
import org.grayrabbit.cms.util.IpUtil;
import org.grayrabbit.cms.vo.category.CategoryBean;
import org.grayrabbit.cms.vo.content.ContentAttachBean;
import org.grayrabbit.cms.vo.content.ContentBean;
import org.grayrabbit.cms.vo.system.OperLogBean;
import org.grayrabbit.dao.util.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

/**
 * <b>ContentAction</b> Struts2 action??





 * @version 1.0,创建时间:2011-05-16 14:40:11 
 */
@Controller
@Scope("prototype")
public class ContentAction extends BaseAction<ContentBean> {
	private static final long serialVersionUID = -8841925129187084862L;
	public static final String NOAUDIT = "noaudit";
	public static final String INDEX = "index";
	
	
	/**
	 * 专题中添加文章跳转result名
	 */
//	public static final String TOPIC = "topic";
	
	//Service接口接入
	@Autowired
	@Qualifier("contentServiceImpl")
	private ContentService contentService;
	
	@Autowired
	@Qualifier("contentAttachServiceImpl")
	private ContentAttachService contentAttachService;
	
	@Autowired
	@Qualifier("categoryServiceImpl")
	private CategoryService categoryService;
	
	@Autowired
	@Qualifier("dictionaryServiceImpl")
	private DictionaryService dictionaryService;
	
	@Autowired
	@Qualifier("topicServiceImpl")
	private TopicService topicService;
	@Autowired
	@Qualifier("adminServiceImpl")
	private AdminService adminService;
	
	@Autowired
	@Qualifier("operLogServiceImpl")
	private OperLogService operLogService;
	
	
	@Autowired
	@Qualifier("configServiceImpl")
	private ConfigService configService;
		
	//VO对象，由前端页面数据组装
	private ContentBean content;
	private List<Dictionary> dicContentType;
	//右边审核页面的列表选项
	private List<Dictionary> dicContentType2;
	private List<Dictionary> dicIsTop;
	private List<Dictionary> dicAudit;
	private List<Dictionary> dicIsComment;
	private List<Dictionary> dicPublishType;
	private List<Dictionary> dicContentTp;
	private String contextType;
	private ContentAttachBean contentAttach;
	private String cflag;
	private Integer tflag;
	private String showfla;
	private Integer indexflag;
	private String keycont;
	private List<CategoryBean> cbList;
	private String adminLoginName;
	private Topic topic;
	private OperLog operLog = new OperLog();
	private List<OperLog> operLogList;
	private List<Category> categoryList;
	private String contentIds;
	private List<String> contenttpllist=new ArrayList<String>();
	private List<String> toptpllist=new ArrayList<String>();//Repeat
	private List<String> checkRepeatList=new ArrayList<String>();
	private List<Content> contentList ;
	private String status;
	private String contentCondition;
	private String submitTime;
	private String listId;
	private ContentTypes[] contentTypes = ContentTypes.values(); 
	
	private String conType;
	

	public String getConType() {
		return conType;
	}

	public void setConType(String conType) {
		this.conType = conType;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}


	//批量操作时使用的传入的ID
	private String ids;
	
	
	public List<Dictionary> getDicPublishType() {
		return dicPublishType;
	}

	public void setDicPublishType(List<Dictionary> dicPublishType) {
		this.dicPublishType = dicPublishType;
	}

	public String getIds() {
		return ids;
	}
 
	public void setIds(String ids) {
		this.ids = ids;
	}
	
	public String getContentIds() {
		return contentIds;
	}

	public void setContentIds(String contentIds) {
		this.contentIds = contentIds;
	}

	public List<Category> getCategoryList() {
		return categoryList;
	}

	public void setCategoryList(List<Category> categoryList) {
		this.categoryList = categoryList;
	}

	public List<Dictionary> getDicIsComment() {
		return dicIsComment;
	}

	public void setDicIsComment(List<Dictionary> dicIsComment) {
		this.dicIsComment = dicIsComment;
	}
	public List<OperLog> getOperLogList() {
		return operLogList;
	}

	public void setOperLogList(List<OperLog> operLogList) {
		this.operLogList = operLogList;
	}

	public Topic getTopic() {
		return topic;
	}

	public void setTopic(Topic topic) {
		this.topic = topic;
	}
	
	
	public String getAdminLoginName() {
		return adminLoginName;
	}

	public void setAdminLoginName(String adminLoginName) {
		this.adminLoginName = adminLoginName;
	}

	public List<CategoryBean> getCbList() {
		return cbList;
	}

	public void setCbList(List<CategoryBean> cbList) {
		this.cbList = cbList;
	}

	public List<Dictionary> getDicAudit() {
		return dicAudit;
	}

	public void setDicAudit(List<Dictionary> dicAudit) {
		this.dicAudit = dicAudit;
	}

	public String getCflag() {
		return cflag;
	}

	public void setCflag(String cflag) {
		this.cflag = cflag;
	}

	public Integer getTflag() {
		return tflag;
	}

	public void setTflag(Integer tflag) {
		this.tflag = tflag;
	}
	public String getShowfla() {
		return showfla;
	}

	public void setShowfla(String showfla) {
		this.showfla = showfla;
	}

	public String getKeycont() {
		return keycont;
	}

	public void setKeycont(String keycont) {
		this.keycont = keycont;
	}
	public ContentAttachBean getContentAttach() {
		return contentAttach;
	}

	public void setContentAttach(ContentAttachBean contentAttach) {
		this.contentAttach = contentAttach;
	}

	public List<Dictionary> getDicIsTop() {
		return dicIsTop;
	}

	public void setDicIsTop(List<Dictionary> dicIsTop) {
		this.dicIsTop = dicIsTop;
	}

	public String getContextType() {
		return contextType;
	}

	public void setContextType(String contextType) {
		this.contextType = contextType;
	}

	public List<Dictionary> getDicContentType() {
		return dicContentType;
	}

	public void setDicContentType(List<Dictionary> dicContentType) {
		this.dicContentType = dicContentType;
	}
	


	public List<String> getCheckRepeatList() {
		return checkRepeatList;
	}

	public void setCheckRepeatList(List<String> checkRepeatList) {
		this.checkRepeatList = checkRepeatList;
	}

	public List<String> getToptpllist() {
		return toptpllist;
	}

	public void setToptpllist(List<String> toptpllist) {
		this.toptpllist = toptpllist;
	}

	public List<String> getContenttpllist() {
		return contenttpllist;
	}

	public void setContenttpllist(List<String> contenttpllist) {
		this.contenttpllist = contenttpllist;
	}

	/**return 前端页面数据组装VO对象,<b>ContentBean</b>*/
	public ContentBean getContent() {
		return content;
	}

	public void setContent(ContentBean content) {
		this.content = content;
	}
	

	public List<Content> getContentList() {
		return contentList;
	}

	public void setContentList(List<Content> contentList) {
		this.contentList = contentList;
	}
	
	//分页对象
	private Page<Content> page = new Page<Content>(MAX_PAGE_NUM,true);
	
	
	/*
	 * return分页对象，默认实例化。如：page.pageNu=2,将更新相应的对象property
	 */
	public Page<Content> getPage() {
		return page;
	}
	//接收前端form,url提交参数组装成分页property
	public void setPage(Page<Content> page) {
		this.page = page;
	}
	
	public HttpSession getCurrentSession() {
		HttpSession session = null;
		session = ServletActionContext.getRequest().getSession();
		return session;
	}
	
	/**
	 * 在执行（struts2的action调用时）list()方法前执行本方法
	 */
	public void prepareList(){
		prepareModel();
	}
	
	/**
	 * 在执行任何其它action方法（struts2的action调用时）前执行本方法
	 */
	public void prepare(){
		if(content==null){
			content = new ContentBean();
		}
	}
	/**
	 * 实现ModelDriven的接口简例前端页的bean组装写法,
	 *  如：
	 *	<pre>
	 *		<code> name="user.userName"</code>
	 *	改写成：
	 *		<code> name="userName"</code>
	 *	action实例中，会自动组成user成员变量
	 *	</pre>
	 */
	public ContentBean getModel() {
		return content;
	}
	
	@Override
	protected void prepareModel(){
		if(content!=null&&StringUtils.isNotBlank(content.getContentId())){
			Content obj = contentService.getContent(content.getContentId());
			content = (ContentBean)dozer.map(obj,ContentBean.class);
		}
		if(content!=null&&content.getCategory()!=null&&StringUtils.isNotBlank(content.getCategory().getCategoryId())){
			content.setCategory((CategoryBean)dozer.map(categoryService.getCategory(content.getCategory().getCategoryId()), CategoryBean.class));
		}
		
			
	}
	/**
	 * 批量操作，ID处理
	 * @return
	 */
	private String[] getBatchIds(){
		String[] idBatchs=null;
		if((null!=ids)&&(StringUtils.isNotBlank(ids))){
			if(ids.contains("on")){
				idBatchs=ids.substring(3).split("@");
			}else{
				idBatchs=ids.split("@");
			}
			
		}
		return idBatchs;
	}
	/**
	 * 删除<b>Content</b>记录的方法，成员变量<b>key</b>为主键的String数组，以主键方式进行删作
	 * 删除成功后，返回"reload"，然后跳转到content-list.action
	 */
	@Override
	@Action(value="content-delete",
			results={@Result(name="reload",type="redirectAction",params={"actionName","content-list?content.category.categoryId=${content.category.categoryId}"}),
					 @Result(name="topic",type="redirectAction",params={"actionName","content-list?topic.topicId=${topic.topicId}&tflag=1"})}
			)
	public String delete(){
		String[] idBatchs=getBatchIds();
		if(topic!=null&&StringUtils.isNotBlank(topic.getTopicId())){
			if(idBatchs != null && idBatchs.length>0){
				contentService.delete(idBatchs);
			}else{
				contentService.delete(key);
			}
			return TOPIC;
		}else{
			content = (ContentBean)dozer.map(contentService.getContent(key[0]), ContentBean.class);
			contentService.delete(key);
			return RELOAD;
		}
	}
	
	/**
	 * Content的列表方法，通过成员变量<b>page??{insClassName}</b>调用service接口，更新page
	 * 执行成功后，将映射到content-list.jsp页面
	 */
	@Override
	@Action(value="content-list",
		results={@Result(name="reload",location="/WEB-INF/content/content/content-category.jsp"),
				 @Result(name="topic",location="/WEB-INF/content/topic/topic-contentmanager.jsp"),
				 @Result(name="success",location="/WEB-INF/content/content/content-list.jsp") 
				}
	)
	public String list(){
		setSessionValue(JUMP_FLAG, "");
		if(topic!=null){
			if(content==null){
				content = new ContentBean();
			}
			if(topic != null){
				topic = topicService.getTopic(topic.getTopicId());
				content.setTopic(topic);
			}
			adminLoginName = ((Admin)getSessionValue(ConstantWebApp.CURRENT_ADMIN)).getLoginName();
			showfla = "1";
			if(tflag == 2){
				content.setTflag("shenhe");
			}
			if(StringUtils.isNotBlank(conType)){
				content.setDocType(conType);
			}
			contentService.list(page,content);
			if(dictionaryService.getDictionaryListByTypeCode(DictionaryStaticValues.CONTENT_STATUS)!=null){
				dicContentType = dictionaryService.getDictionaryListByTypeCode(DictionaryStaticValues.CONTENT_STATUS);
			}
			if(toptpllist.size()==0){
				 if(topic!=null){
					topic=topicService.getTopic(topic.getTopicId());
					String[] CatContentTplSet3=null;
					if(StringUtils.isNotBlank(topic.getTopicDefaultContentTpl())){
						if(topic.getTopicDefaultContentTpl().contains("-")){
							CatContentTplSet3=topic.getTopicDefaultContentTpl().split("-");
							for (String ss : CatContentTplSet3) {
								toptpllist.add(ss);
							}
						}else{
							toptpllist.add(topic.getTopicDefaultContentTpl());
						}		
					}
					checkRepeatList=null;
					checkRepeatList=toptpllist;
					String[] CatContentTplSet4=null;
					if(StringUtils.isNotBlank(topic.getTopicContentTplSet())){
						if(topic.getTopicContentTplSet().contains("-")){
							CatContentTplSet4=topic.getTopicContentTplSet().split("-");
							for (String ss : CatContentTplSet4) {
								boolean topicFlag=false;
								for(int i=0;i<checkRepeatList.size();i++){
									if(checkRepeatList.get(i).equals(ss)){
										topicFlag=true;
									}									
								}
								if(topicFlag==false){
									toptpllist.add(ss);
								}
							}
						}else{
							
							String conset=topic.getTopicContentTplSet();
							for(int i=0;i<checkRepeatList.size();i++){
								if(checkRepeatList.get(i).equals(conset)){
									break;
								}else{
									toptpllist.add(conset);
								}
							}
							
						}	
					}
				}
			}
			if(dictionaryService.getDictionaryListByTypeCode(DictionaryStaticValues.CONTENT_TYPE)!=null){
				dicContentTp = dictionaryService.getDictionaryListByTypeCode(DictionaryStaticValues.CONTENT_TYPE);
			}
			return TOPIC;
		}else{
			List<Category> catList = categoryService.findByParentId(content.getCategory().getCategoryId(), null);
			if(!DBInitId.CMS_SUPPER_ADMIN_ID.equals(getCurrentAdminId())){
				@SuppressWarnings("unchecked")
				Set<String> authSet = (Set<String>)getSessionValue(ConstantWebApp.CURRENT_ADMIN_AUTH_CODE);
				List<Category> result= new ArrayList<Category>();			
				for(Category c : catList){
					if(result.contains(c)) continue; 
					for(String s : authSet){
						if(s.equals(ConstantOperCode.CAT_DOC_AUDIT+"-"+c.getCategoryId()) 
								||s.equals(ConstantOperCode.CAT_DOC_EDIT+"-"+c.getCategoryId())
								||s.equals(ConstantOperCode.CAT_DOC_UNLOCK+"-"+c.getCategoryId())){
							result.add(c);
							 break;
						}
					}
				}
				catList = result;
			}			
			
			if(catList.size()>0&&catList!=null){
				if(cbList==null){
					cbList = new ArrayList<CategoryBean>();
				}
				for(Category item:catList){
					CategoryBean cb = (CategoryBean)dozer.map(item,CategoryBean.class);
					ContentBean itemContentBean = new ContentBean();
					itemContentBean.setCategory(item);
					itemContentBean.setContentStatus(null);
					cb.setContentCount(contentService.getCount(itemContentBean));
					itemContentBean.setContentStatus(DictionaryStaticValues.CONTENT_STATUS_NEW);
					cb.setAuditContentCount(contentService.getCount(itemContentBean));
					itemContentBean.setContentStatus(DictionaryStaticValues.CONTENT_STATUS_PUBLISH);
					cb.setPublishContentCount(contentService.getCount(itemContentBean));
					cbList.add(cb);
				}
				return RELOAD;
			}else{
				content.setCategory(categoryService.getCategory(content.getCategory().getCategoryId()));
				if(contenttpllist.size()==0){
					if(content.getCategory()!=null){
						String[] CatContentTplSet1=null;
						if(StringUtils.isNotBlank(content.getCategory().getCatDefaultContentTpl())){
							if(content.getCategory().getCatDefaultContentTpl().contains("-")){
								CatContentTplSet1=content.getCategory().getCatDefaultContentTpl().split("-");
								for (String ss : CatContentTplSet1) {
									contenttpllist.add(ss);
								}
							}else{
								contenttpllist.add(content.getCategory().getCatDefaultContentTpl());
							}		
						}
						checkRepeatList=null;
						checkRepeatList=contenttpllist;					
						String[] CatContentTplSet2=null;
						if(StringUtils.isNotBlank(content.getCategory().getCatContentTplSet())){
							if(content.getCategory().getCatContentTplSet().contains("-")){
								CatContentTplSet2=content.getCategory().getCatContentTplSet().split("-");
								for (String ss : CatContentTplSet2) {
									boolean conFlag=false;
									for(int i=0;i<checkRepeatList.size();i++){	
										if(checkRepeatList.get(i).equals(ss)){
											conFlag=true;
										}
									}
									if(conFlag==false){
										contenttpllist.add(ss);
									}
								
								}
							}else{						
								String catconset=content.getCategory().getCatContentTplSet();
							
									for(int i=0;i<checkRepeatList.size();i++){
										if(checkRepeatList.get(i).equals(catconset)){
											break;
										}
										else{
											contenttpllist.add(catconset);
										}
									}							
							}	
						}
					}	
				} 
				content.setSiteId(getCurrentSiteId());
				contentService.list(page,content);
				if(dictionaryService.getDictionaryListByTypeCode(DictionaryStaticValues.CONTENT_STATUS)!=null){
					dicContentType = dictionaryService.getDictionaryListByTypeCode(DictionaryStaticValues.CONTENT_STATUS);
				}
				adminLoginName = ((Admin)getSessionValue(ConstantWebApp.CURRENT_ADMIN)).getLoginName();
				return SUCCESS;
			}
		}
	}
	@Action(value="content-listcat")
	public String listCategory(){
		String info = "{catId:'";
		Site currentSite = (Site)getSessionValue(ConstantWebApp.CURRENT_SITE);
		categoryList =categoryService.findBySiteId(currentSite.getSiteId());
		String catIds = "";
		String catNames = "";
		for(Category obj:categoryList){
			catIds+=obj.getCategoryId()+"@";
			catNames+=obj.getCatName()+"@";
		}
		info+=catIds;
		info+="',catName:'";
		info+=catNames;
		info+="'}";
		return renderText(info);
	}
	
	@Action(value="content-move")
	public String moveContent(){
		String[] idBatchs=null;
		String text="yes";
		if((null!=contentIds)&&(StringUtils.isNotBlank(contentIds))){
			if(contentIds.contains("on")){
				idBatchs=contentIds.substring(3).split(",");
			}else{
				idBatchs=contentIds.split(",");
			}
			
		}
		for(int j=0;j<idBatchs.length;j++){
			Content obj = contentService.getContent(idBatchs[j]);
			obj.setCategory(content.getCategory());
			try {
				contentService.save(obj, (Admin)getSessionValue(ConstantWebApp.CURRENT_ADMIN), null);
			} catch (Exception e) {
				text="no";
				e.printStackTrace();
			}
		}
		return renderText(text);
	}

	/**
	 * 查询除了本专题以外其他所有文章列表
	 * @return
	 */
	@Action(value="content-listnotin",
			results={@Result(name="topic",location="/WEB-INF/content/topic/topic-contentmanager.jsp")}
		)
	public String listNotIn(){
		if(content==null){
			content = new ContentBean();
		}
		if(topic != null){
			topic = topicService.getTopic(topic.getTopicId());
			content.setTopic(topic);
		}
		try{
			if(StringUtils.isNotBlank(content.getKeywords())){
				String keywds = new String(content.getKeywords().getBytes("ISO-8859-1"),"UTF-8");
				content.setKeywords(keywds);
			}
		}catch(Exception e){
			
		}
		keycont = "1";
		contentService.listNotIn(page, content);
		if(dictionaryService.getDictionaryListByTypeCode(DictionaryStaticValues.CONTENT_STATUS)!=null){
			dicContentType = dictionaryService.getDictionaryListByTypeCode(DictionaryStaticValues.CONTENT_STATUS);
		}
		return TOPIC;
	}
	@Action(value="content-selecttpl",
			results={@Result(name="topic",location="/WEB-INF/content/topic/topic-tpl.jsp")}
		)
	public String selectTpl(){
		topic=topicService.getTopic(topic.getTopicId());
		content.setTopic(topic);
		contentService.listNotIn(page, content);
		if(dictionaryService.getDictionaryListByTypeCode(DictionaryStaticValues.CONTENT_STATUS)!=null){
			dicContentType = dictionaryService.getDictionaryListByTypeCode(DictionaryStaticValues.CONTENT_STATUS);
		}
		return TOPIC;
	}
	
	/**
	 * 查询指定·专题下未审核的文章
	 * @return
	 */
	@Action(value="content-listnotcheck",
			results={@Result(name="topic",location="/WEB-INF/content/topic/topic-contentmanager.jsp")}
		)
	public String listNotCheck(){
		contentService.list(page, content);
		if(dictionaryService.getDictionaryListByTypeCode(DictionaryStaticValues.CONTENT_STATUS)!=null){
			dicContentType = dictionaryService.getDictionaryListByTypeCode(DictionaryStaticValues.CONTENT_STATUS);
		}
		return TOPIC;
	}
	/**
	 * 查询当前管理员有权限访问栏目中未审核的文章
	 * @return
	 */
	@Action(value="content-auditcontent",
			results={@Result(name="noaudit",location="/WEB-INF/content/content/auditcontent-list.jsp")}
		)
	public String findByCats(){
		Admin admin = getCurrentAdmin();
		if(dictionaryService.getDictionaryListByTypeCode(DictionaryStaticValues.CONTENT_STATUS)!=null){
			dicContentType2 = dictionaryService.getDictionaryListByTypeCode(DictionaryStaticValues.CONTENT_STATUS);
			//2011-12-07 蔡万东  去除下拉列表的发布状态和锁定状态
			if (dicContentType2.size() >= 5) {
				if ("发布状态".equals(dicContentType2.get(3).getDictionaryValue())) {
					dicContentType2.remove(3);
				}
				if ("锁定".equals(dicContentType2.get(3).getDictionaryValue())) {
					dicContentType2.remove(3);
				}
			}
			//结束
			
		}
		List<String> catList = new ArrayList<String>();
		if(DBInitId.CMS_SUPPER_ADMIN_ID.equals(admin.getAdminId()) && StringUtils.isNotBlank(admin.getAdminId())){
			content.setAdminId(DBInitId.CMS_SUPPER_ADMIN_ID);
			content.setCatIds(catList);
			contentService.list(page, content);
		}else if(StringUtils.isNotBlank(admin.getAdminId()) && StringUtils.isNotBlank(getCurrentSiteId())){
			List<Category> list = categoryService.findBySiteId(getCurrentSiteId());
			if(list.isEmpty()){//栏目列表为空，直接返回
				return NOAUDIT;
			}
			Set<String> authSet = this.getAuthSet();
			if(authSet.isEmpty()){//权限为空，直接返回
				return NOAUDIT;
			}
			for(String s:authSet){
				if(s.startsWith(ConstantOperCode.CAT_DOC_AUDIT)){
					Category c = new Category(s.split("\\-")[1]);
					if(list.contains(c)){
						catList.add(s.split("\\-")[1]);
					}
				}
			}				
			content.setCatIds(catList);
			contentService.list(page, content);
		}
		return NOAUDIT;
	}
	
	/**
	 * Content的新增与编辑的方法执行成功能
	 * 将映射到content-input.jsp
	 */
	@Action(value="content-input",
			results={@Result(name="success",location="content-input.jsp")})
	public String input(){
			Admin admin = (Admin)getSessionValue(ConstantWebApp.CURRENT_ADMIN);
			if(content!=null && StringUtils.isNotBlank(content.getContentId())){
				operLog.setOperObjectId(content.getContentId());	
			}else{
				content.setCreateTime(new Date());
				content.setUpdateTime(new Date());
			}
			if(StringUtils.isNotBlank(cflag)){
				dicAudit = dictionaryService.getDictionaryListByTypeCode(DictionaryStaticValues.AUDIT_TYPE);
				operLogList = operLogService.getLogInfos((OperLogBean)dozer.map(operLog,OperLogBean.class));	
			}
			prepareModel();
			if(dictionaryService.getDictionaryListByTypeCode(DictionaryStaticValues.IS_TOP)!=null){
				dicIsTop = dictionaryService.getDictionaryListByTypeCode(DictionaryStaticValues.IS_TOP);
			}
			if(dictionaryService.getDictionaryListByTypeCode(DictionaryStaticValues.IS_COMMENT)!=null){
				dicIsComment = dictionaryService.getDictionaryListByTypeCode(DictionaryStaticValues.IS_COMMENT);
			}
			if(dictionaryService.getDictionaryListByTypeCode(DictionaryStaticValues.CONTENT_PUBLISH_TYPE)!=null){
				dicPublishType = dictionaryService.getDictionaryListByTypeCode(DictionaryStaticValues.CONTENT_PUBLISH_TYPE);
			}
			if(content!=null&&StringUtils.isNotBlank(content.getContentId())){
				
				Content c = contentService.getContent(content.getContentId());
				//原状态，直接返回的继续保存为该状态
				status = c.getContentStatus();
				contentService.save(c, (Admin)getSessionValue(ConstantWebApp.CURRENT_ADMIN), DictionaryStaticValues.CONTENT_STATUS_LOCK);
				operLogService.addLog(admin.getLoginName(), admin.getRealName(),Module.CONTENT.getValue(),
						OperAct.LOCK.getValue(), content.getContentId(), IpUtil.getClientIp(getRequest()));
			}
			
			if(contenttpllist.size()==0){
				if(content.getCategory()!=null){
					String[] CatContentTplSet1=null;
					if(StringUtils.isNotBlank(content.getCategory().getCatDefaultContentTpl())){
						if(content.getCategory().getCatDefaultContentTpl().contains("-")){
							CatContentTplSet1=content.getCategory().getCatDefaultContentTpl().split("-");
							for (String ss : CatContentTplSet1) {
								contenttpllist.add(ss);
							}
						}else{
							contenttpllist.add(content.getCategory().getCatDefaultContentTpl());
						}		
					}
					checkRepeatList=contenttpllist;					
					String[] CatContentTplSet2=null;
					if(StringUtils.isNotBlank(content.getCategory().getCatContentTplSet())){
						if(content.getCategory().getCatContentTplSet().contains("-")){
							CatContentTplSet2=content.getCategory().getCatContentTplSet().split("-");
							for (String ss : CatContentTplSet2) {
								boolean conFlag=false;
								for(int i=0;i<checkRepeatList.size();i++){	
									if(checkRepeatList.get(i).equals(ss)){
										conFlag=true;
									}
								}
								if(conFlag==false){
									contenttpllist.add(ss);
								}
							
							}
						}else{						
							String catconset=content.getCategory().getCatContentTplSet();
						
								for(int i=0;i<checkRepeatList.size();i++){
									if(checkRepeatList.get(i).equals(catconset)){
										break;
									}
									else{
										contenttpllist.add(catconset);
									}
								}
							
														
						}	
					}
				}	
			}
			
			if(toptpllist.size()==0){
				 if(topic!=null){
					topic=topicService.getTopic(topic.getTopicId());
					String[] CatContentTplSet3=null;
					if(StringUtils.isNotBlank(topic.getTopicDefaultContentTpl())){
						if(topic.getTopicDefaultContentTpl().contains("-")){
							CatContentTplSet3=topic.getTopicDefaultContentTpl().split("-");
							for (String ss : CatContentTplSet3) {
								toptpllist.add(ss);
							}
						}else{
							toptpllist.add(topic.getTopicDefaultContentTpl());
						}		
					}
					checkRepeatList=toptpllist;
					String[] CatContentTplSet4=null;
					if(StringUtils.isNotBlank(topic.getTopicContentTplSet())){
						if(topic.getTopicContentTplSet().contains("-")){
							CatContentTplSet4=topic.getTopicContentTplSet().split("-");
							for (String ss : CatContentTplSet4) {
								boolean topicFlag=false;
								for(int i=0;i<checkRepeatList.size();i++){
									if(checkRepeatList.get(i).equals(ss)){
										topicFlag=true;
									}									
								}
								if(topicFlag==false){
									toptpllist.add(ss);
								}
							}
						}else{
							
							String conset=topic.getTopicContentTplSet();
							for(int i=0;i<checkRepeatList.size();i++){
								if(checkRepeatList.get(i).equals(conset)){
									break;
								}else{
									toptpllist.add(conset);
								}
							}
							
						}	
					}
				}
			}
			return SUCCESS;
		}
	
	/**
	 * save or update Content的方法，将成员property content转成Content实例，执行保存；
	 * 执行成功后，返回"reload"，然后跳转到content-list.action
	 */
	@Override
	@Action(value="content-save",
			results={
					@Result(name="site",type="redirectAction",params={"actionName","content-site"}), 
					@Result(name="reload",type="redirectAction",params={"actionName","content-list?content.category.categoryId=${content.category.categoryId}"}),
					@Result(name="topic",type="redirectAction",params={"actionName","content-list?topic.topicId=${topic.topicId}&tflag=1"}),
					@Result(name="index",type="redirectAction",params={"actionName","content-auditcontent"})}
		)
	public String save(){
		Content obj = (Content) dozer.map(content, Content.class);
		obj.setSiteId(getCurrentSiteId());
		if(!StringUtils.isNotBlank(obj.getContentId())){
			obj.setContentId(null);		
			obj.setIsLocked(Boolean.FALSE);
			operLog.setOperAct(OperAct.ADD.getValue());
			if(topic != null&&StringUtils.isNotBlank(topic.getTopicId())){
				Topic t = topicService.getTopic(topic.getTopicId());
				if(t.getIsAudit()){
					obj.setContentStatus(DictionaryStaticValues.CONTENT_STATUS_NEW);					
				}else {
					obj.setContentStatus(DictionaryStaticValues.CONTENT_STATUS_SUCCESS);		
				}
			}else if(content.getCategory() != null&&StringUtils.isNotBlank(content.getCategory().getCategoryId())){
				Category  c = categoryService.getCategory(content.getCategory().getCategoryId());
				if(c.getIsAudit()){
					obj.setContentStatus(DictionaryStaticValues.CONTENT_STATUS_NEW);					
				}else {
					obj.setContentStatus(DictionaryStaticValues.CONTENT_STATUS_SUCCESS);		
				}
			}
			
		}else{
			if("1".equals(cflag)){
				operLog.setOperAct(OperAct.ADUIT.getValue());
			}else{
				operLog.setOperAct(OperAct.EDIT.getValue()); 
				obj.setPublishSug(null);
			}
		}
		Admin admin = (Admin)getSessionValue(ConstantWebApp.CURRENT_ADMIN);
		if(topic != null&&StringUtils.isNotBlank(topic.getTopicId())){			
			obj.setTopic(topic);
		} 
		//1 list+index+content
		//2 content
		if(StringUtils.isNotBlank(content.getAndPub())){
			contentService.save(obj,admin,DictionaryStaticValues.CONTENT_STATUS_PUBLISH);
			contentAttachService.updateContentAttach(contentAttach.getAttachId(), contentAttach.getAttachMemo(),obj);
			operLogService.addLog(admin.getLoginName(), admin.getRealName(), Module.CONTENT.getValue(), 
					OperAct.ADD_PUBLISH.getValue(), obj.getContentId(), IpUtil.getClientIp(getRequest()));
			publishContents(obj,!"1".equals(content.getAndPub()));
		}
		
		else{
			contentService.save(obj,admin,cflag);
			contentAttachService.updateContentAttach(contentAttach.getAttachId(), contentAttach.getAttachMemo(),obj);
			operLogService.addLog(admin.getLoginName(), admin.getRealName(), Module.CONTENT.getValue(),
					operLog.getOperAct(), obj.getContentId(), IpUtil.getClientIp(getRequest()));
		}
		if(ContentStatic.cab!=null){
			ContentStatic.cab.remove(obj.getContentId());
		}
		
		String jump = (String)getSessionValue(JUMP_FLAG);
		if(StringUtils.isBlank(jump)){
			if(indexflag!=null&&indexflag==1){
				return INDEX;
			} 
			if(topic != null && StringUtils.isNotBlank(topic.getTopicId())){
				return TOPIC;
			}else{
				return RELOAD;
			}			
		}else{
			return "site";
		}
		
	}
	
	/**
	 * 发布文章
	 * @return
	 */
	@Action(value="content-publish",
			results={@Result(name="reload",type="redirectAction",params={"actionName","content-list?content.category.categoryId=${content.category.categoryId}"}),
			 @Result(name="topic",type="redirectAction",params={"actionName","content-list?topic.topicId=${topic.topicId}&tflag=1"})}
		)
	public String publish(){
		Admin admin = (Admin)getSessionValue(ConstantWebApp.CURRENT_ADMIN);
		contentService.save(contentService.getContent(content.getContentId()),admin,cflag);
		operLogService.addLog(admin.getLoginName(), admin.getRealName(), Module.CONTENT.getValue(),OperAct.PUBLISH.getValue(), content.getContentId(), IpUtil.getClientIp(getRequest()));
		if(topic != null && StringUtils.isNotBlank(topic.getTopicId())){
			return TOPIC;
		}else{
			return RELOAD;
		}
	}
	/**
	 * 解除锁定
	 * @return
	 */
	@Action(value="content-unlock",
		results={
			@Result(name="site",type="redirectAction",params={"actionName","content-site"}),
			@Result(name="reload",type="redirectAction",params={"actionName","content-list?content.category.categoryId=${content.category.categoryId}"}),
			@Result(name="topic",type="redirectAction",params={"actionName","content-list?topic.topicId=${topic.topicId}&tflag=1"})}
	)
	public String unlock(){
		String jumpFlag = (String)getSessionValue(JUMP_FLAG);
		Admin admin = (Admin)getSessionValue(ConstantWebApp.CURRENT_ADMIN);
		contentService.save(contentService.getContent(content.getContentId()),
				(Admin) getSessionValue(ConstantWebApp.CURRENT_ADMIN),
				DictionaryStaticValues.CONTENT_STATUS_UNLOCK);
		operLogService.addLog(admin.getLoginName(), admin.getRealName(), Module.CONTENT.getValue(),
				OperAct.UNLOCK.getValue(), content.getContentId(), IpUtil.getClientIp(getRequest()));		
		if(topic != null && StringUtils.isNotBlank(topic.getTopicId())){			 
			if(StringUtils.isBlank(jumpFlag)) return TOPIC;
			else  return "site";
		}else{
			if(StringUtils.isBlank(jumpFlag)) return RELOAD;
			else  return "site";
		}
	}
	@Action(value="content-ajaxunlock")
	public String ajaxunlock(){
		Admin admin = (Admin)getSessionValue(ConstantWebApp.CURRENT_ADMIN);
		if(StringUtils.isNotBlank(status)){
			contentService.save(contentService.getContent(content.getContentId()),(Admin)getSessionValue(ConstantWebApp.CURRENT_ADMIN),status.trim());
		}
		else{
			contentService.save(contentService.getContent(content.getContentId()),(Admin)getSessionValue(ConstantWebApp.CURRENT_ADMIN),null);
		}
		operLogService.addLog(admin.getLoginName(), admin.getRealName(), Module.CONTENT.getValue(),
				OperAct.UNLOCK.getValue(), content.getContentId(), IpUtil.getClientIp(getRequest()));
		return null;
	}
	
	
	
	/**
	 * 获取数据字典KEY值对应的NAME
	 * 
	 * @param key
	 * @param type
	 *            数据字典类型
	 * @return
	 */
	public String getName(String key,String code) {
		return dictionaryService.getDictionaryByDictionaryKey(key,code).getDictionaryValue();
	}
	
	/**
	 * 根据文章id获得栏目名称
	 * @param contentId
	 * @return
	 */
	public String getCatName(String contentId){
		return contentService.getCatName(contentId);
	}
	
	/**
	 * 获取文章状态
	 * @param contentFlag
	 * @return
	 */
	public static String getContentStatus(String contentFlag){
		if(contentFlag.equals(DictionaryStaticValues.CONTENT_STATUS_NEW)){
			return DictionaryStaticValues.CONTENT_STATUS_NEW;
		}else if(contentFlag.equals(DictionaryStaticValues.CONTENT_STATUS_SUCCESS)){
			return DictionaryStaticValues.CONTENT_STATUS_SUCCESS;
		}else if(contentFlag.equals(DictionaryStaticValues.CONTENT_STATUS_FAIL)){
			return DictionaryStaticValues.CONTENT_STATUS_FAIL;
		}else{
			return DictionaryStaticValues.CONTENT_STATUS_PUBLISH;
		}
	}
	
	/**
	 * 获取操作名称，状态为1或2时（审核通过，或审核未通过），操作名为重审，其他为审核
	 * @param contentStatus
	 * @return
	 * @author 蔡万东
	 */
	public  String getOperate(String contentStatus){
		if("1".equals(contentStatus)||"2".equals(contentStatus))
		    return "重审";
		else
			return "审核";
	}
	
	@Action(value="content-batch")
	public String batch(){
		Admin admin = (Admin)getSessionValue(ConstantWebApp.CURRENT_ADMIN);
//		String action = null;
		for(int i = 0 ; i<content.getContentId().split(",").length ; i ++){
			Content obj =contentService.getContent(content.getContentId().split(",")[i]);
			if(content.getIsComment()!=null){
				obj.setIsComment(content.getIsComment());
//				action = "推荐";
			}
			if(content.getIsTop()!=null){
				obj.setIsTop(content.getIsTop());
//				action = "置顶";
			}
			if(StringUtils.isNotBlank(content.getShowType())){
				obj.setShowType(content.getShowType());
//				action = "展示";
			}
			if(StringUtils.isNotBlank(content.getContentTpl())){
				obj.setContentTpl(content.getContentTpl());
//				action = "模板";
			}
//			action = "修改";
			contentService.save(obj, (Admin)getSessionValue(ConstantWebApp.CURRENT_ADMIN), null);
			operLogService.addLog(admin.getLoginName(), admin.getRealName(), Module.CONTENT.getValue(),
					OperAct.EDIT.getValue(), content.getContentId().split(",")[i], IpUtil.getClientIp(getRequest()));
		}
		return renderText("sure");
	}
	
	/**
	 * 文章标题已存在验证
	 * @return
	 */
	@Action(value="content-checknameunique")
	public String  checkNameUnique(){
		String text="";
		try{
			Boolean unique=contentService.checkNameUnique(content.getContentTitle());
			if(!unique){
				text="ok";
			}else{
				text="no";
			}
		}catch(Exception e){
			text="no";
		}
		return renderText(text);
	}
	
	/**
	 * 判断当前管理员是否对栏目有审核权限
	 * @return
	 */
	public boolean isAudit(String categoryId){ 
		if(StringUtils.isBlank(categoryId)) return false;		
		String id = getCurrentAdminId();
		if(DBInitId.CMS_SUPPER_ADMIN_ID.equals(id))
			return true;
		List<Admin> list = adminService.AllAuditorAdmins(
				ConstantOperCode.AUTH_CAT_TYPE, categoryId);
		if(list == null || list.isEmpty()) return false;
		for(Admin a : list){
			if(a.getAdminId().equals(id))
				return true;
		}
		return false; 
	}
	
	public Integer getIndexflag() {
		return indexflag;
	}

	public void setIndexflag(Integer indexflag) {
		this.indexflag = indexflag;
	}
	
	private void publishContents(Content obj,boolean onlyContent){
		
			log.info("发布文章出错结束.......");		
	}


	public String getContentCondition() {
		return contentCondition;
	}

	public void setContentCondition(String contentCondition) {
		this.contentCondition = contentCondition;
	}

	public String getSubmitTime() {
		return submitTime;
	}

	public void setSubmitTime(String submitTime) {
		this.submitTime = submitTime;
	}

	public String getListId() {
		return listId;
	}

	public void setListId(String listId) {
		this.listId = listId;
	}


	public List<Dictionary> getDicContentType2() {
		return dicContentType2;
	}

	public void setDicContentType2(List<Dictionary> dicContentType2) {
		this.dicContentType2 = dicContentType2;
	}
	
	public String formatDate(Date date){ 
		return DateUtil.formatDateToString(date, "yyyy-MM-dd HH:mm:ss");
	}
	
	@Action(value="content-site",results={
			@Result(name="site" ,location="content-site.jsp")
	})
	public String siteContent(){
		setSessionValue(JUMP_FLAG, "site");
		dicContentType = dictionaryService.getDictionaryListByTypeCode(DictionaryStaticValues.CONTENT_STATUS);	 
		content.setSiteId(getCurrentSiteId());
		content.setCatIdset(hasAuthCategoryIds());
	    contentService.listSite(page, content); 	    
		return "site";
	}
	
	private Set<String> hasAuthCategoryIds(){
		if(DBInitId.CMS_SUPPER_ADMIN_ID.equals(getCurrentAdminId())){
			return null;
		}		
		Set<String> set = new HashSet<String>();
		List<Category>  list= CategoryCache.
				getAuthCategories_content(getCurrentAdminId()+"_"+getCurrentSiteId());
		for(Category c: list){
			set.add(c.getCategoryId());
		}
		return set;
	}

	public ContentTypes[] getContentTypes() {
		return contentTypes;
	}

	public void setContentTypes(ContentTypes[] contentTypes) {
		this.contentTypes = contentTypes;
	}

	public List<Dictionary> getDicContentTp() {
		return dicContentTp;
	}

	public void setDicContentTp(List<Dictionary> dicContentTp) {
		this.dicContentTp = dicContentTp;
	}
 
	
	
}